const app = getApp()
const login = require('../../utils/login.js')
Page({
  data: {
    hasUserInfo: false,
    canIUse: false,
    imgs: {
      index1: app.globalData.apiImgBase2 + 'index1.png',
      index2: app.globalData.apiImgBase2 + 'index2.png',
      index3: app.globalData.apiImgBase2 + 'index3.png',
      index4: app.globalData.apiImgBase2 + 'index4.png',
      index5: app.globalData.apiImgBase2 + 'index5.png'
    },
    toView: 'item3',
    userInfo: {
      avatarUrl: '../../img/avatar.png',
      nickName:''
    },
    accountInfo: {
      balance: 0
    }
  },
  
  onLoad() {
    let that = this
    login.getIfLogin(this)
    this.getAccountInfo()
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        console.log(res)
        if(res.data!=''){
          that.setData({
            userInfo: res.data
          })
        }
      }
    })
  },
  onTabItemTap(){
    this.getAccountInfo()
    login.getIfLogin(this)
  },
  scroll(e) {
    console.log(e)
  },
  getUserInfo: function (e) {
    var that = this
    console.log(e)
    if (e.detail.userInfo) {
      wx.setStorageSync("userInfo", e.detail.userInfo);
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
      console.log("点击了同意授权");

      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          if (res.code) {
            console.log(res.code)
            wx.setStorageSync('code', res.code)
            wx.getUserInfo({
              success: ress => {
                console.log(ress)
                let data = {
                  "code": res.code,
                  "headImage": that.data.userInfo.avatarUrl,
                  "nickName": that.data.userInfo.nickName,
                  "userInfo": JSON.stringify(that.data.userInfo),
                  "encryptedData": ress.encryptedData,
                  "iv": ress.iv,
                  "appType":"yqs"
                }
                console.log(data)
                app.request('oauth/token', data, 'POST').then(item => {
                  console.log(item)
                  if (item.success) {
                    
                    wx.hideLoading()
                    wx.setStorageSync('token', item.content)
                    that.setData({
                      canIUse: true
                    })
                    that.getAccountInfo()
                  } else {
                    wx.hideLoading()
                    that.setData({
                      hasUserInfo: false
                    })
                  }
                })
              }
            })
          } else {
            console.log('获取用户登录失败！' + res.errMsg)
            that.setData({
              hasUserInfo: false
            })
          }
        },
        fail: res => {
          that.setData({
            hasUserInfo: false
          })
        }
      })

    }
  },
  getAccountInfo() {
    app.request('ugc/getAccountInfo').then(res => {
      console.log(res)
      wx.hideLoading()
      if (res.success) {
        this.setData({
          accountInfo: res.content
        })
      } else {
        console.log(res.message)
      }
    })
  },
  bindDetail(e){
    wx.navigateTo({
      url:'../detail/detail?type='+e.currentTarget.dataset.id+'&page=0'
    })
  },
  onShareAppMessage(){
    var that = this;
    return {
      title: '来自'+this.data.userInfo.nickName+'的邀请-娱骑士',
      imageUrl: '../../img/share_bg.jpg'
    };
  }
})
